<template lang="html">
  <el-dialog :title="title+'管理员'" v-model="visible" size="tiny" textarea>
    <el-form :model="form" ref="form" :rules="rules" label-width="100px">
      <el-form-item label="管理员账号:" :label-width="formLabelWidth" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="管理员密码:" :label-width="formLabelWidth" prop="username">
        <el-input type="password" v-model="form.userpassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码:" :label-width="formLabelWidth" prop="username">
        <el-input type="password" v-model="form.userpassword1"></el-input>
      </el-form-item>
      <el-form-item label="昵称:" :label-width="formLabelWidth" prop="phone">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="邮箱:" :label-width="formLabelWidth" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="创建时间:" :label-width="formLabelWidth" prop="created_at">
        <div class="block">
          <el-date-picker v-model="form.created_at" type="datetime" placeholder="选择创建时间">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="状态:" :label-width="formLabelWidth">
        <el-radio class="status" v-model="form.status" label="1">有效</el-radio>
        <el-radio class="status" v-model="form.status" label="0">无效</el-radio>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth">
        <el-button type="primary" @click="confirmBtn()">确定</el-button>
        <el-button @click="cancleBtn()">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
// import 'babel-polyfill';
import * as admin from '../../../api/admin.js'
import myUpload from 'vue-image-crop-upload/upload-2.vue';
import {
  get_filter_params,
  reset_filter_params,
  // get_api_branch_http_host,
  datetime_format,
  form_init
} from '../../../helpers.js'
export default {
  props: {
    value: {
      type: Boolean,
      default: false
    },
    info: Object
  },
  data() {
    return {
      visible: this.value,
      formLabelWidth: '105px',
      upload: '',
      show: false,
      form: {
        id: null,
        username: null,
        userpassword:null,
        userpassword1:null,
        nickname: '',
        phone: '',
        email: '',
        created_at: '',
        status: ''
      },
      params:{
        filed_type: 'admin'
      },
      rules: {

      }
    }
  },
  // created() {
  //   this.upload = get_api_branch_http_host() + '/upload';
  //   // console.log(this.upload);
  // },
  methods: {
    // toggleShow() {
    //   this.show = !this.show;
    // },
    // cropSuccess(imgDataUrl, field) {
    //   // this.form.image = imgDataUrl;
    // },
    // cropUploadSuccess(data, field) {
    //   this.form.file_id = data.id;
    //   this.form.image = data.path;
    // },
    // cropUploadFail(status, field) {
    //   console.log('----Fail-----');
    // },
    cancleBtn(){
      this.visible = false;
      this.$message.info('操作取消');
    },
    confirmBtn() {
      if (this.info) {
        admin.update(this.form.id, this.form).then(data => {
          var date2 = new Date(this.form.created_at);
          var localeString = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate() + " " + date2.getHours() + ":" + date2.getMinutes() + ":" + date2.getSeconds();
          this.form.created_at = localeString;
          var date1 = new Date(this.form.updated_at);
          var localeString1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate() + " " + date1.getHours() + ":" + date1.getMinutes() + ":" + date1.getSeconds();
          this.form.updated_at = localeString1;
          this.$message.success('编辑成功');
          this.$emit('submit', this.form);
        })
        this.visible = false;
      } else {
        admin.store(this.form).then(data => {
          let date2 = new Date(this.form.created_at);
          let localeString = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate() + " " + date2.getHours() + ":" + date2.getMinutes() + ":" + date2.getSeconds();
          this.form.created_at = localeString;
          let date1 = new Date(this.form.updated_at);
          let localeString1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate() + " " + date1.getHours() + ":" + date1.getMinutes() + ":" + date1.getSeconds();
          this.form.updated_at = localeString1;
          this.$message.success('新增成功');
          this.$emit('submit', this.form);
        })
        this.visible = false;
      }
    }
  },
  computed: {
    title() {
      return this.info ? "编辑" : "新增";
    }
  },
  components: {
    myUpload
  },
  watch: {
    value(val) {
      this.visible = val;
    },
    visible(val) {
      this.$emit('input', val);
    },
    info(info) {
      form_init(info, this.form);
    }
  }
}
</script>
<style lang="css">
</style>
